package Servlets;

import java.io.File;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import ServerDB.User;

/**
 * 
 * @author Guy & Liel
 *
 */
public class LoginServlet extends HttpServlet
{
	private static final long serialVersionUID = 1L;

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out=response.getWriter();
		out.println("<HTML>");
		out.println("<HEAD>");
		System.out.println("Check insert registration");
		String userName=request.getParameter("UserNameTextBox").toString();
		String password=request.getParameter("PasswordTextBox").toString();
		File file=new File("C:\\ourDropBox\\hibernate.cfg.xml");
		SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();
		Session session=sessionFactory.openSession();
		String strQuery=" SELECT * FROM tbl_users WHERE USER_NAME = :userNameQry and PASSWORD = :passwordQry";
		Query query=session.createSQLQuery(strQuery);
		query.setParameter("userNameQry", userName);
		query.setParameter("passwordQry", password);
		System.out.println("After SQL line");
		@SuppressWarnings("unchecked")
		List<User> listUsers=query.list();
		Iterator<User> itUser=listUsers.iterator();

		if(itUser.hasNext())//Response The user name is in DataBase
		{
			HttpSession httpSession=request.getSession(true);
			httpSession.setAttribute("Login", userName);
			out.println("<meta HTTP-EQUIV='REFRESH' content='0; url=afterLogin.jsp'>");


		}		
		else
		{

			System.out.println("User name / password incorrect");
			HttpSession httpSession=request.getSession(true);
			httpSession.setAttribute("error user name or password", "error user name or password");
			out.println("<meta HTTP-EQUIV='REFRESH' content='0; url=home.jsp'>");
		}

		out.println("</HEAD>");
		out.println("<BODY>");
		out.println("</BODY>");
		out.println("</HTML>");
		out.close();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException
	{
		ArrayList<String> errorList=new ArrayList<String>();
		String userName=request.getParameter("userName").toString();
		String password=request.getParameter("password").toString();
		File file=new File("C:\\ourDropBox\\hibernate.cfg.xml");
		SessionFactory sessionFactory=new Configuration().configure(file).buildSessionFactory();
		Session session=sessionFactory.openSession();
		String strQuery=" SELECT * FROM tbl_users WHERE USER_NAME = :userNameQry and PASSWORD = :passwordQry";
		Query query=session.createSQLQuery(strQuery);
		query.setParameter("userNameQry", userName);
		query.setParameter("passwordQry", password);
		System.out.println("After SQL line");
		@SuppressWarnings("unchecked")
		List<User> listUsers=query.list();
		Iterator<User> itUser=listUsers.iterator();
		
		if(!itUser.hasNext())//Response The user name is in DataBase
		{
			errorList.add("UserName/Password is invalid");
		}
		ObjectOutputStream out=new ObjectOutputStream(response.getOutputStream());
		out.writeObject(errorList);
		out.close();



	}
}
